Socket
Socket
Sign inDemoInstall

micro-memoize

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro-memoize

A tiny, crazy fast memoization library for the 95% use-case


Version published
Weekly downloads
352K
increased by1.69%
Maintainers
1
Weekly downloads
 
Created

What is micro-memoize?

micro-memoize is a tiny, performant memoization library for JavaScript. It is designed to cache the results of function calls to improve performance, especially for expensive or frequently called functions. The library is highly configurable and supports various options for customizing the memoization behavior.

What are micro-memoize's main functionalities?

Basic Memoization

This feature allows you to memoize a function so that its results are cached. Subsequent calls with the same arguments will return the cached result instead of recomputing.

const memoize = require('micro-memoize');

const expensiveFunction = (num) => {
  console.log('Computing...');
  return num * num;
};

const memoizedFunction = memoize(expensiveFunction);

console.log(memoizedFunction(5)); // Computing... 25
console.log(memoizedFunction(5)); // 25 (cached result)

Custom Cache Size

This feature allows you to set a custom cache size. Once the cache exceeds this size, the oldest cached results are discarded.

const memoize = require('micro-memoize');

const expensiveFunction = (num) => {
  console.log('Computing...');
  return num * num;
};

const memoizedFunction = memoize(expensiveFunction, { maxSize: 2 });

console.log(memoizedFunction(5)); // Computing... 25
console.log(memoizedFunction(6)); // Computing... 36
console.log(memoizedFunction(5)); // 25 (cached result)
console.log(memoizedFunction(7)); // Computing... 49
console.log(memoizedFunction(6)); // 36 (cached result)
console.log(memoizedFunction(5)); // Computing... 25 (cache size exceeded, recomputed)

Custom Equality Function

This feature allows you to provide a custom equality function to determine if two arguments are equivalent. This is useful for complex data structures where simple equality checks are insufficient.

const memoize = require('micro-memoize');

const expensiveFunction = (obj) => {
  console.log('Computing...');
  return obj.value * obj.value;
};

const isEqual = (a, b) => a.value === b.value;

const memoizedFunction = memoize(expensiveFunction, { isEqual });

console.log(memoizedFunction({ value: 5 })); // Computing... 25
console.log(memoizedFunction({ value: 5 })); // 25 (cached result)

Other packages similar to micro-memoize

Keywords

FAQs

Package last updated on 27 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc